1 00:00:00,350 --> 00:00:01,340 Welcome back. 2 00:00:01,370 --> 00:00:06,170 The next thing we need to do in our story is to start a countdown for completing the tasks, and then 3 00:00:06,170 --> 00:00:10,550 we will wait for either the players to complete all their tasks, or for the countdown to reach zero. 4 00:00:10,580 --> 00:00:15,200 Once that happens, we want to flicker the lights inside the building and lose power to continue our 5 00:00:15,200 --> 00:00:15,830 story. 6 00:00:16,160 --> 00:00:17,390 So let's get started. 7 00:00:17,420 --> 00:00:22,010 After we have told our task service to set up all of the tasks, that means we're going to have to start 8 00:00:22,010 --> 00:00:22,970 a countdown. 9 00:00:23,000 --> 00:00:28,430 So what I'm going to do is I'm going to create a for loop, and this is going to loop for a number of 10 00:00:28,430 --> 00:00:28,910 seconds. 11 00:00:28,910 --> 00:00:33,170 So we can go back up here and we can create a constant section real quick. 12 00:00:34,710 --> 00:00:40,380 And we can create a variable to represent how long we want to give the players to complete their tasks. 13 00:00:40,380 --> 00:00:42,630 So we can call this time for tasks. 14 00:00:42,630 --> 00:00:49,230 And we can do something like 100 and 20s then we can go back down to our for loop, and we could start 15 00:00:49,230 --> 00:00:51,960 the index at our time for tasks. 16 00:00:51,960 --> 00:00:54,090 And we want to go down all the way to zero. 17 00:00:54,090 --> 00:00:56,400 And we want to subtract by one each time. 18 00:00:59,430 --> 00:01:03,750 And what we're going to do in here is we're going to use our update GUI event, and we want to fire 19 00:01:03,750 --> 00:01:09,540 to all of the clients, and we want to give them the action in our GUI actions enum of updating the 20 00:01:09,540 --> 00:01:10,380 countdown. 21 00:01:10,380 --> 00:01:15,660 And then what we want to do is we want to give them a number that is equal to the index. 22 00:01:15,660 --> 00:01:18,810 And then we're going to yield for one second every single time. 23 00:01:18,810 --> 00:01:20,820 So we're going to start at 100 and 20s. 24 00:01:20,820 --> 00:01:23,340 And this is going to continually count down. 25 00:01:23,340 --> 00:01:33,750 Now we need to either wait for this countdown to end, or we need to wait for the game comms event to 26 00:01:33,750 --> 00:01:38,340 fire back to us so the task service can let us know that all the players completed their tasks. 27 00:01:38,340 --> 00:01:41,040 So what I'm going to do is I'm actually going to create two variables up here. 28 00:01:41,040 --> 00:01:46,200 I'm going to call it timed out and another one called Completed Tasks. 29 00:01:46,200 --> 00:01:48,120 And these are going to be two booleans. 30 00:01:48,120 --> 00:01:50,610 This will let us know whether or not we timed out. 31 00:01:50,610 --> 00:01:54,570 And this will let us know if all the players completed their tasks or not. 32 00:01:54,570 --> 00:02:00,060 And then what we're going to do is every single time in this for loop, we're going to check if we timed 33 00:02:00,060 --> 00:02:04,260 out or if we completed our tasks. 34 00:02:04,260 --> 00:02:10,800 If we did, then we can send out a warning like the timer was set to zero. 35 00:02:11,420 --> 00:02:18,410 And then we can use our update guy event and fire to all of the clients to set their timers to zero. 36 00:02:18,410 --> 00:02:23,030 So update countdown and we can set the number. 37 00:02:24,260 --> 00:02:32,330 Equal to zero, then what we would want to do is we would also want to listen for our event to happen, 38 00:02:32,330 --> 00:02:35,540 or wait until our loop is done. 39 00:02:35,630 --> 00:02:41,840 In order to do this, we could create a function that would get passed an event, and it will also get 40 00:02:41,840 --> 00:02:42,890 passed a time. 41 00:02:42,890 --> 00:02:48,230 And this function will sit there and either wait for the event to fire or for the timeout to occur. 42 00:02:48,230 --> 00:02:52,670 So what I'm going to do is I'm going to go up and I'm going to make a new private function, and I'm 43 00:02:52,670 --> 00:02:55,850 going to call this private function wait for event. 44 00:02:55,850 --> 00:03:01,070 And what we're going to do is we're going to pass this function, an event which is an ARB script signal. 45 00:03:01,070 --> 00:03:04,580 And we're also going to get passed a number to represent the timeout. 46 00:03:06,800 --> 00:03:11,810 And what we're going to do in this function is we're going to get the current running thread that is 47 00:03:11,810 --> 00:03:17,450 executing here in this function, and we can get it by referring to our coroutine library and executing 48 00:03:17,450 --> 00:03:22,550 the running function, which returns the current running coroutine, or basically the thread that is 49 00:03:22,550 --> 00:03:24,470 executing in our function. 50 00:03:24,620 --> 00:03:29,360 Then what we're also going to do is I'm going to make a connection to our event, because we're going 51 00:03:29,360 --> 00:03:31,250 to be connecting a function to this event. 52 00:03:31,250 --> 00:03:33,830 So we can call this event connection. 53 00:03:33,830 --> 00:03:36,260 And for the time being we're going to set this to nil. 54 00:03:37,090 --> 00:03:41,830 Then we can refer to our event connection and make it equal to this event. 55 00:03:41,830 --> 00:03:45,340 And we're going to connect a lambda function to this event. 56 00:03:45,640 --> 00:03:49,510 And then we're also going to use our task dot delay function. 57 00:03:49,510 --> 00:03:51,490 And we're going to pass the time out here. 58 00:03:51,490 --> 00:03:54,130 And we're also going to be executing a lambda function in here. 59 00:03:54,130 --> 00:03:58,150 So we're going to have both of these listening simultaneously. 60 00:03:58,150 --> 00:04:00,580 This function right here is going to be counting down. 61 00:04:00,580 --> 00:04:03,820 And this one is going to be sitting here waiting for this event to fire. 62 00:04:03,820 --> 00:04:09,160 So either this timeout will happen first or this event will get fired first. 63 00:04:09,160 --> 00:04:15,100 Either way we're going to yield in this function until either the event fires or the timeout is reached. 64 00:04:15,100 --> 00:04:19,780 Because what we're going to do is we're going to return at the end of our script, but we're going to 65 00:04:19,780 --> 00:04:22,870 return using the coroutine dot yield function. 66 00:04:22,870 --> 00:04:29,680 And this is going to pause or suspend the current thread inside of our wait for event function. 67 00:04:29,680 --> 00:04:35,710 And that means we aren't able to resume until we're actually told to resume using the coroutine dot 68 00:04:35,710 --> 00:04:36,910 resume function. 69 00:04:36,910 --> 00:04:42,670 So when we call this function, it's going to sit there and yield until we either have this event fire 70 00:04:42,670 --> 00:04:44,440 or this timeout is reached. 71 00:04:44,440 --> 00:04:48,280 So for example, let's say the event fires what we could do. 72 00:04:48,870 --> 00:04:52,200 Is we could use the coroutine dot resume function. 73 00:04:52,200 --> 00:04:58,500 We can pass our current thread that is going to be paused to resume it, and when we resume it, that 74 00:04:58,500 --> 00:05:03,150 means it's going to go back to this yield function and it's going to return whatever gets passed to 75 00:05:03,150 --> 00:05:03,930 the yield function. 76 00:05:03,930 --> 00:05:07,920 And whatever gets passed to this yield function, we can pass right here. 77 00:05:08,040 --> 00:05:11,670 So if the event gets fired, that means we didn't time out. 78 00:05:11,670 --> 00:05:14,880 So we can pass a boolean of false. 79 00:05:14,880 --> 00:05:18,720 And we can also pass whatever information gets passed from this event. 80 00:05:18,720 --> 00:05:22,800 So we could create three dots here to represent a variadic amount of arguments. 81 00:05:22,800 --> 00:05:27,360 And then we can pass those arguments along with the coroutine dot resume function. 82 00:05:27,360 --> 00:05:30,960 We can also do the exact same thing here on our task dot delay function. 83 00:05:30,960 --> 00:05:34,440 We'll just copy this and we're going to resume the current thread. 84 00:05:34,440 --> 00:05:38,820 But instead we're going to return true to signify that we did time out. 85 00:05:38,820 --> 00:05:39,360 Right. 86 00:05:39,360 --> 00:05:41,820 This function executed before this function. 87 00:05:41,820 --> 00:05:43,800 Now let's say this function runs. 88 00:05:43,800 --> 00:05:45,510 And then a second later this function runs. 89 00:05:45,510 --> 00:05:49,860 How do we stop this function from also calling the coroutine dot resume function? 90 00:05:49,860 --> 00:05:56,400 Well, what we could do for example inside of this lambda function, if the event gets fired, we can 91 00:05:56,400 --> 00:06:00,090 get our event connection and we can disconnect it. 92 00:06:00,090 --> 00:06:03,150 And then we can set the event connection equal to nil. 93 00:06:03,150 --> 00:06:08,400 And this will allow us to know whether or not either of these lambda functions already disconnected, 94 00:06:08,400 --> 00:06:09,750 and set this variable to nil. 95 00:06:09,750 --> 00:06:15,330 So we can have an if statement in here to check if the event connection is equal to nil. 96 00:06:15,330 --> 00:06:20,970 If it is, then we're just going to return out of the function because the other function already resumed 97 00:06:20,970 --> 00:06:21,900 our current thread. 98 00:06:21,900 --> 00:06:25,890 And we can basically just copy this and do the exact same check in here. 99 00:06:25,890 --> 00:06:29,550 And we'll copy this and do the exact same thing in here. 100 00:06:29,550 --> 00:06:33,570 So either this function here will resume our thread and return false. 101 00:06:33,570 --> 00:06:37,290 Or this function right here will resume our thread and return true. 102 00:06:37,870 --> 00:06:45,070 So now what we could do is we could refer to timed out and completed tasks and make it equal to whatever 103 00:06:45,070 --> 00:06:47,470 gets returned from our wait for event function. 104 00:06:47,470 --> 00:06:53,230 And the event we want to listen to is our game comms event or our game comms Rebindable. 105 00:06:53,230 --> 00:06:54,970 So game comms bindable. 106 00:06:55,670 --> 00:06:58,850 And we're going to wait for this to get fired. 107 00:06:58,970 --> 00:07:02,630 And then we're also going to pass our time for tasks here. 108 00:07:02,630 --> 00:07:08,480 So this function is either going to wait for this Bindable event to get fired, or it's going to wait 109 00:07:08,480 --> 00:07:10,010 for our timeout to occur. 110 00:07:10,010 --> 00:07:13,400 And this boolean will let us know whether or not we timed out. 111 00:07:13,400 --> 00:07:17,330 And then we also pass from this event the action. 112 00:07:17,330 --> 00:07:23,090 So if you remember, we go back to our task service when we completed all of our tasks. 113 00:07:23,330 --> 00:07:25,490 Um, let me figure this out here, okay. 114 00:07:25,490 --> 00:07:25,700 Yeah. 115 00:07:25,700 --> 00:07:26,300 Right here. 116 00:07:26,300 --> 00:07:32,450 When we complete our tasks, we fire, the game comes Bindable event, and we pass this action, which 117 00:07:32,450 --> 00:07:33,290 is a string. 118 00:07:33,290 --> 00:07:40,460 And since a string when put inside of a condition will be truthy, that means if we check what this 119 00:07:40,460 --> 00:07:43,100 variable is, if it's a string, that means it's going to be truthy. 120 00:07:43,100 --> 00:07:47,030 But if this event never got fired, then that means this is going to be nil or Falsy. 121 00:07:47,060 --> 00:07:52,340 Now we're only going to be able to use this function if we actually spawn this for loop in another thread, 122 00:07:52,340 --> 00:07:57,080 because currently the thread of execution will be stuck in this for loop, and it won't be able to go 123 00:07:57,080 --> 00:07:58,820 to our wait for event function. 124 00:07:58,970 --> 00:08:01,400 So we need to use task dot spawn. 125 00:08:02,060 --> 00:08:07,490 And spawn a new function here, and then we can just take our for loop and put it inside of there. 126 00:08:08,950 --> 00:08:12,040 And now we spawn our countdown. 127 00:08:12,040 --> 00:08:16,810 And then immediately afterwards, we're waiting for our game Combinable event to get fired. 128 00:08:16,810 --> 00:08:19,030 And we're also waiting for the timeout. 129 00:08:19,030 --> 00:08:22,540 So either we'll time out or this event's going to get fired. 130 00:08:22,540 --> 00:08:27,670 And when that happens, this will be set to either true or false, or this will be set to true or false. 131 00:08:27,670 --> 00:08:30,070 So if we timed out this will be true. 132 00:08:30,070 --> 00:08:31,090 And this will be false. 133 00:08:31,780 --> 00:08:35,110 Or if we didn't time out this will be false and this will be truthy. 134 00:08:35,110 --> 00:08:38,260 Which is why we're checking both conditions and our for loop. 135 00:08:39,120 --> 00:08:44,940 Either way, after we have timed out or completed all of our tasks, we're going to use the game Combinable 136 00:08:44,940 --> 00:08:51,780 and we're going to fire and give the action game comms enum server dot task service, and we're going 137 00:08:51,780 --> 00:08:54,330 to tell it to disable all of the tasks. 138 00:08:54,900 --> 00:08:58,110 And then what we could do is we could check whether or not we completed the tasks. 139 00:08:58,110 --> 00:09:05,040 So if we completed tasks then we're going to go into our sections game sections, dot objectives, dot 140 00:09:05,040 --> 00:09:08,760 tasks and set the fulfilled boolean to true. 141 00:09:09,390 --> 00:09:13,830 And then we're going to use our update guy event and fire to all of the clients that we have fulfilled 142 00:09:13,830 --> 00:09:14,490 an objective. 143 00:09:14,490 --> 00:09:17,250 So Guy actions enum dot objective fulfilled. 144 00:09:17,250 --> 00:09:20,460 And that objective is going to be the tasks objective. 145 00:09:20,460 --> 00:09:26,340 So the fulfilled objective name is equal to tasks. 146 00:09:27,590 --> 00:09:31,850 Otherwise, if we didn't complete our tasks, then we're just going to do the exact same thing here, 147 00:09:31,850 --> 00:09:38,480 but instead we're going to tell them that they failed an objective so we can say objective failed. 148 00:09:38,480 --> 00:09:42,830 And then the failed objective name is also going to be tasks. 149 00:09:43,400 --> 00:09:48,050 So what we can do now is we can actually go ahead and test out whether or not our countdown is working. 150 00:09:48,050 --> 00:09:50,630 So let's go in here and play test this. 151 00:09:51,740 --> 00:09:52,130 All right. 152 00:09:52,130 --> 00:09:54,290 Hopefully we get our timer showing up here. 153 00:09:57,490 --> 00:10:00,220 And we did get all the tasks set up. 154 00:10:00,220 --> 00:10:04,300 And unfortunately, it looks like we do not have a timer going on on these screens. 155 00:10:04,300 --> 00:10:08,050 So we're going to have to figure out why real quick. 156 00:10:09,250 --> 00:10:14,590 Oh, and the reason being is because we actually never filled out our update countdown function. 157 00:10:14,590 --> 00:10:16,540 That's actually the next thing we need to do. 158 00:10:16,540 --> 00:10:18,670 So this will be pretty easy. 159 00:10:18,670 --> 00:10:21,460 Remember we get passed a number in this args table. 160 00:10:21,460 --> 00:10:25,390 So we'll get the num equal to args dot number. 161 00:10:26,260 --> 00:10:30,280 And then if this num is not equal to zero. 162 00:10:30,950 --> 00:10:36,740 And if the countdown label we have dot text transparency is equal to one. 163 00:10:37,430 --> 00:10:44,570 Then what we need to do is we need to fade our text onto the screen so we could use the fade element 164 00:10:44,570 --> 00:10:47,300 function, pass our count down label. 165 00:10:47,300 --> 00:10:50,000 We can do something like 0.25 seconds. 166 00:10:50,000 --> 00:10:56,300 And we want to make it completely opaque because by default, our count down label is not going to be, 167 00:10:56,300 --> 00:10:57,650 uh, clearly visible. 168 00:10:57,650 --> 00:10:59,030 It's going to be transparent. 169 00:10:59,060 --> 00:11:01,940 We also need to make sure that our countdown label is visible. 170 00:11:01,940 --> 00:11:04,880 So countdown label dot visibility is equal to true. 171 00:11:05,360 --> 00:11:11,090 And then what we could do is we could set the countdown label dot text equal to this number. 172 00:11:12,210 --> 00:11:19,320 And then inside of the sound service inside of our guy folder, there is a tick sound, and we're going 173 00:11:19,320 --> 00:11:21,360 to go ahead and play our tick sound. 174 00:11:21,360 --> 00:11:26,100 And then afterwards, the last thing we need to check is if our number is zero. 175 00:11:26,100 --> 00:11:32,220 So if the number is equal to zero, then that means our countdown is over and we should fade our countdown 176 00:11:32,220 --> 00:11:37,980 number back off of the screen so we can use the fade element function again to get our countdown label. 177 00:11:37,980 --> 00:11:40,530 And we want to make the duration like one second. 178 00:11:40,530 --> 00:11:42,960 And we want to fade it out to be completely transparent. 179 00:11:42,960 --> 00:11:47,730 And then after that we can set the countdown label that visibility back to false. 180 00:11:47,730 --> 00:11:50,610 And that's all we need to do for our countdown function. 181 00:11:51,190 --> 00:11:55,030 So now if we test this out again, our countdown should actually be working. 182 00:11:57,930 --> 00:11:59,040 And perfect. 183 00:11:59,040 --> 00:12:04,080 Now, as you can see, our countdown is up on top of the screen, showing us how much time we have left 184 00:12:04,080 --> 00:12:05,910 to complete our tasks. 185 00:12:05,910 --> 00:12:10,890 So what we can actually do is we can actually go ahead and test to see if our event, first of all, 186 00:12:10,890 --> 00:12:11,310 is working. 187 00:12:11,310 --> 00:12:13,920 So let's go ahead and complete our tasks first of all. 188 00:12:17,390 --> 00:12:21,050 And studio, for some reason, is having this weird lagging problem. 189 00:12:21,050 --> 00:12:22,940 As you can see that my mouse is moving around. 190 00:12:22,940 --> 00:12:25,340 Just another weird bug unfortunately, I'm gonna have to deal with. 191 00:12:26,900 --> 00:12:34,220 But we can get our tasks done here and see if our countdown fades out and all of our tasks get disabled. 192 00:12:36,800 --> 00:12:38,960 So here we're completed our last task. 193 00:12:38,960 --> 00:12:40,700 And when this gets finished here. 194 00:12:42,200 --> 00:12:42,740 There we go. 195 00:12:42,740 --> 00:12:49,670 Our timer has been disabled, but unfortunately, it looks like our for loop is still executing because 196 00:12:49,670 --> 00:12:52,700 I think we forgot to break out of the for loop. 197 00:12:53,090 --> 00:12:55,850 So let's go ahead and fix that real quick. 198 00:12:56,300 --> 00:12:56,780 Right. 199 00:12:56,780 --> 00:13:01,880 So after we have timed out or we have completed the tasks, we set the timer to zero. 200 00:13:01,880 --> 00:13:07,340 And afterwards we actually need to go ahead and break out of this for loop because we do not need to 201 00:13:07,340 --> 00:13:08,480 do the count anymore. 202 00:13:08,510 --> 00:13:08,900 Okay. 203 00:13:08,900 --> 00:13:09,440 Perfect. 204 00:13:09,440 --> 00:13:10,190 Fix that. 205 00:13:10,190 --> 00:13:15,800 Otherwise it seems to be working well and we can go ahead and continue what we need to do next in our 206 00:13:15,800 --> 00:13:16,520 story. 207 00:13:16,520 --> 00:13:21,530 The next thing I want to do is that after either the timeout occurs or the players complete their tasks, 208 00:13:21,530 --> 00:13:25,880 is we want to start flickering the lights in the building until we lose power. 209 00:13:25,880 --> 00:13:29,960 And we're also going to have Squidward talk in comment about the flickering lights. 210 00:13:30,140 --> 00:13:36,080 So that means we can add some new text for Squidward here in our messages table, so we can create a 211 00:13:36,080 --> 00:13:39,800 section called Squidward two, and we'll have some messages in here. 212 00:13:39,800 --> 00:13:42,380 Like we could say, oh, great. 213 00:13:43,320 --> 00:13:47,670 The storm is messing with the power. 214 00:13:47,910 --> 00:13:53,760 And then we could say something like, hopefully we don't lose power completely. 215 00:13:54,030 --> 00:13:57,960 And then after he says these, we're going to continue flickering the lights for a little bit until 216 00:13:57,960 --> 00:14:00,780 we actually disable the power completely in the building. 217 00:14:00,780 --> 00:14:06,030 And afterwards we can have them say another set of texts so we can call this Squidward three and he 218 00:14:06,030 --> 00:14:11,250 can give some comments like, uh, we'll just put a bunch of dots so he'll be like, what? 219 00:14:11,250 --> 00:14:13,710 And then he'll say, wow. 220 00:14:14,600 --> 00:14:17,360 And then he'll say something like, I'm going. 221 00:14:18,010 --> 00:14:20,800 To check the breaker in the basement. 222 00:14:20,800 --> 00:14:25,600 And after he says that, we're also going to be putting some glow sticks behind them, that the players 223 00:14:25,600 --> 00:14:32,110 can go ahead and collect so we can give them a message like there's some glow sticks behind me if you 224 00:14:32,110 --> 00:14:33,040 need them. 225 00:14:34,540 --> 00:14:40,180 And then he's going to say, stay here and don't do anything stupid. 226 00:14:40,960 --> 00:14:43,720 And that's all the messages we need him to say for now. 227 00:14:44,110 --> 00:14:46,570 And now we're going to need another few functions. 228 00:14:46,570 --> 00:14:49,090 We're of course going to need a function. 229 00:14:49,090 --> 00:14:51,700 We can call it flicker lights. 230 00:14:52,540 --> 00:14:57,040 We're going to need a function to actually turn off the power in the building so we can call it turn 231 00:14:57,040 --> 00:14:58,090 off power. 232 00:14:58,870 --> 00:15:02,710 And basically what we're going to do is we're going to call this function to start flickering the lights. 233 00:15:02,710 --> 00:15:07,810 So inside of here, what we can do is we can first create a variable, and we're going to collect all 234 00:15:07,810 --> 00:15:10,720 of the flickering sounds that we have in the sound service. 235 00:15:10,720 --> 00:15:12,760 So we can call it flicker sounds. 236 00:15:12,940 --> 00:15:14,980 It's in the sound service. 237 00:15:14,980 --> 00:15:19,210 And that means we're actually going to have to grab the sound service inside of the game section. 238 00:15:19,210 --> 00:15:20,740 So let's go get that real quick. 239 00:15:20,740 --> 00:15:24,850 Sound service is equal to game get service sound service. 240 00:15:24,850 --> 00:15:31,510 And then we can go back down and inside of the sound service in the ambience folder dot light flicker. 241 00:15:31,510 --> 00:15:33,700 We're going to get all of those sounds in there. 242 00:15:33,850 --> 00:15:36,490 And they're just some generic light flickering sounds. 243 00:15:36,490 --> 00:15:39,370 So if we actually go and take a look at those, we just have two in here. 244 00:15:39,370 --> 00:15:40,510 We could play this one. 245 00:15:41,280 --> 00:15:45,000 Or we could play this one pretty basic. 246 00:15:45,360 --> 00:15:49,890 And what we're going to do inside of here is we'll just have a while loop that executes forever. 247 00:15:49,890 --> 00:15:54,240 And we can have some boolean to keep track of whether or not we can continue flickering the lights until 248 00:15:54,240 --> 00:15:55,350 we turn off the power. 249 00:15:55,350 --> 00:15:57,870 So let's go ahead and create a boolean up here. 250 00:15:57,870 --> 00:16:06,090 We'll just call it something like can Flicker Lights and we'll set that to false as default. 251 00:16:06,090 --> 00:16:12,300 And then when we go down here we want to set can flicker lights equal to true. 252 00:16:12,300 --> 00:16:16,290 And then we're going to use can flicker lights as the condition for this while loop. 253 00:16:16,290 --> 00:16:19,350 So while can flicker lights is true. 254 00:16:19,710 --> 00:16:26,730 What we could do is we could use our game comms event and fire to all of the clients in our game to 255 00:16:26,730 --> 00:16:32,880 play a sound so we could do game comms enum on the client about two clients play sound. 256 00:16:33,550 --> 00:16:37,540 The sound is going to be a random one out of our Flickr sounds folder, so it's going to be equal to 257 00:16:37,540 --> 00:16:43,780 Flickr sounds, and we're just going to use our RNG data type next integer one to the length of our 258 00:16:43,780 --> 00:16:45,190 Flickr sounds table. 259 00:16:45,190 --> 00:16:49,750 And then we're just going to set play at point equal to false. 260 00:16:50,050 --> 00:16:52,570 And then we're just going to wait a random amount of time. 261 00:16:52,570 --> 00:16:59,290 We can do RNG next number way anywhere between 0.5 and 1.5 seconds. 262 00:16:59,650 --> 00:17:04,360 And that means we're going to have to create another function to actually loop through all of the light 263 00:17:04,360 --> 00:17:07,330 instances in the workspace and turn them off. 264 00:17:07,330 --> 00:17:09,910 So we can create a function up here. 265 00:17:09,910 --> 00:17:15,130 We can call it something like change lights, and we'll pass a boolean to it, to whether or not we 266 00:17:15,130 --> 00:17:18,010 want to set the lights on or off so it can be set on. 267 00:17:18,010 --> 00:17:23,920 This will be a boolean, and what we can do inside of this function is we're going to loop through every 268 00:17:23,920 --> 00:17:27,280 single descendant inside of a folder in the workspace. 269 00:17:27,280 --> 00:17:33,070 So in workspace dot krustikrab there's a folder called lights which stores all of the light instances. 270 00:17:33,070 --> 00:17:35,110 So get descendants. 271 00:17:35,260 --> 00:17:40,660 Where we're going to do is we're going to check if this descendant is of the type light. 272 00:17:40,660 --> 00:17:47,470 So if it's a light instance then we need to set the descendant dot enable property equal to the boolean 273 00:17:47,470 --> 00:17:48,790 passed to this function. 274 00:17:50,110 --> 00:17:57,880 And then another thing we need to check is if this descendant is a base part, and we want to check 275 00:17:57,880 --> 00:18:00,490 if this has the material. 276 00:18:00,490 --> 00:18:05,980 So descendant dot material equal to the enum dot material dot neon. 277 00:18:10,260 --> 00:18:16,380 Because if it's a neon part, then we want to either set the color to be black, or we want to set the 278 00:18:16,380 --> 00:18:20,310 color back to its original state if we're turning on or off the lights. 279 00:18:20,310 --> 00:18:22,800 So if the set on variable is false. 280 00:18:22,800 --> 00:18:27,960 So if we need to turn it off, then we can set an attribute on this descendant. 281 00:18:29,150 --> 00:18:30,380 Of original color. 282 00:18:30,380 --> 00:18:34,250 That way we can keep track of what its color originally was, and we'll just pass descendant. 283 00:18:34,430 --> 00:18:35,360 Color here. 284 00:18:35,630 --> 00:18:41,180 And then we're going to set on this descendant dot color equal to a color three dot from RGB. 285 00:18:41,180 --> 00:18:44,270 And it's just going to be black which is 000. 286 00:18:45,340 --> 00:18:50,890 Otherwise, if we want to turn it on, then we can set on this descendant dot color equal to its attributes. 287 00:18:50,890 --> 00:18:53,440 So descendant get attribute. 288 00:18:55,370 --> 00:18:56,870 Our original color. 289 00:18:57,460 --> 00:19:00,310 And that's all we need to do for our change lights function. 290 00:19:00,520 --> 00:19:07,120 So inside of our while loop, while we're continually looping here, we need to basically. 291 00:19:07,950 --> 00:19:12,510 Car, change lights function and pass a boolean to it. 292 00:19:12,540 --> 00:19:18,840 Now the question you might have is how do we alternate the boolean to be true false true false each 293 00:19:18,840 --> 00:19:20,190 time through this while loop? 294 00:19:20,220 --> 00:19:24,360 Well, what we can do is we can create a boolean outside of here. 295 00:19:24,360 --> 00:19:26,100 We're going to set it to false for now. 296 00:19:26,100 --> 00:19:29,520 And we're going to pass this bool to our change lights function. 297 00:19:29,520 --> 00:19:33,180 So through the first iteration of the loop we're going to turn all of the lights off. 298 00:19:33,180 --> 00:19:40,320 And then afterwards what we can do is we can check if our bool is true. 299 00:19:40,320 --> 00:19:44,520 If our bool is true then we can set bool equal to false. 300 00:19:44,520 --> 00:19:47,430 Otherwise we can set bool equal to true. 301 00:19:49,550 --> 00:19:54,620 So this will allow us to alternate our boolean each time through the iteration of our while loop. 302 00:19:54,830 --> 00:20:00,080 And then finally, when we have decided that we flickered the lights enough, we can go ahead and turn 303 00:20:00,080 --> 00:20:01,820 off the power in the building. 304 00:20:01,850 --> 00:20:05,780 So what we would do is we would set can flicker lights equal to false. 305 00:20:05,780 --> 00:20:11,450 And in order to actually, you know, turn off the power, we want to make sure that the lights are 306 00:20:11,450 --> 00:20:16,970 on first off so we can use the change lights function and pass true here. 307 00:20:16,970 --> 00:20:19,130 And we'll also make sure to apply another flicker sound. 308 00:20:19,130 --> 00:20:21,260 So I'll just copy this right here. 309 00:20:23,670 --> 00:20:27,390 And we're also going to have to grab the Flickr sounds as well, so we'll just do that real quick. 310 00:20:28,470 --> 00:20:32,220 So we'll set can flicker lights to false, which will stop this while loop. 311 00:20:32,220 --> 00:20:37,290 We set the lights to true and then after we set the lights to be back on, we're going to wait something 312 00:20:37,290 --> 00:20:40,290 like 0.6 seconds, something like that. 313 00:20:40,290 --> 00:20:43,920 And then we want to change the lights to be back off again. 314 00:20:44,520 --> 00:20:50,370 And then we're going to use our exact same function again to play a sound. 315 00:20:50,370 --> 00:20:56,850 But this time the sound we want to play is going to be inside of the sound service. 316 00:20:57,560 --> 00:20:59,360 Dot ambience. 317 00:20:59,360 --> 00:21:00,470 Dot. 318 00:21:00,710 --> 00:21:02,180 I believe it's outage. 319 00:21:02,180 --> 00:21:02,540 Yeah. 320 00:21:02,540 --> 00:21:03,770 This sound instance. 321 00:21:03,770 --> 00:21:04,790 Let me find it right here. 322 00:21:04,790 --> 00:21:07,520 So when the power goes out we're going to play this sound. 323 00:21:10,870 --> 00:21:11,770 Pretty spooky. 324 00:21:11,770 --> 00:21:18,130 And after we do that inside of server storage, there's a folder called Other Stuff, and there's a 325 00:21:18,130 --> 00:21:20,110 model in there called a glow stick giver. 326 00:21:20,110 --> 00:21:22,870 And we want to set the parent equal to the workspace. 327 00:21:22,870 --> 00:21:24,880 Let me go ahead and take a look at this real quick. 328 00:21:24,880 --> 00:21:27,010 Inside of server storage. 329 00:21:27,010 --> 00:21:27,940 Other stuff. 330 00:21:27,940 --> 00:21:29,410 We have our glow stick giver. 331 00:21:29,410 --> 00:21:32,890 When we put it in the workspace, it's going to appear directly behind Squidward. 332 00:21:32,890 --> 00:21:37,540 And this is going to be a clickable part that the players can click to get glow sticks. 333 00:21:37,540 --> 00:21:41,980 So we're going to have to fill out the function in here to give our players glow sticks, which is going 334 00:21:41,980 --> 00:21:43,060 to be really easy. 335 00:21:43,450 --> 00:21:51,340 The only service we're going to need inside of here is server storage game get service, server storage. 336 00:21:51,640 --> 00:21:54,580 And then we're going to refer to the click detector in our model. 337 00:21:54,580 --> 00:21:59,140 So click detector is equal to the script dot parent. 338 00:21:59,140 --> 00:22:03,130 And then just get the click detector which is in the click part. 339 00:22:03,890 --> 00:22:08,060 And then we're also going to get the glow stick tool we want to give to our players, which is in server 340 00:22:08,060 --> 00:22:10,790 storage, dot tools, dot glow stick. 341 00:22:11,360 --> 00:22:15,080 And then all we're going to do is we're going to listen for when our click detector is clicked. 342 00:22:15,080 --> 00:22:18,470 So mouse click we're going to connect a function and get the player. 343 00:22:19,300 --> 00:22:22,150 And we're going to check whether or not the player already has a glow stick. 344 00:22:22,150 --> 00:22:24,700 So we're going to make a variable called tool. 345 00:22:24,700 --> 00:22:30,610 And we're going to check inside the player dot backpack to see if we can find, uh, first child glow 346 00:22:30,610 --> 00:22:31,330 stick. 347 00:22:31,970 --> 00:22:34,460 Or if it's already on their character. 348 00:22:34,460 --> 00:22:40,490 So or player character find first child glow stick. 349 00:22:42,070 --> 00:22:45,280 If they already have the tool, then we're just going to return. 350 00:22:45,700 --> 00:22:48,040 Otherwise, we can create a clone of our tool. 351 00:22:48,040 --> 00:22:49,930 So glow stick tool clone. 352 00:22:49,930 --> 00:22:54,100 And then we can set the clone dot parent equal to the player's backpack. 353 00:22:54,100 --> 00:22:55,750 And that's all we need to do for this. 354 00:22:56,200 --> 00:22:59,740 So now what we could do is we could go back to our start section one function. 355 00:22:59,740 --> 00:23:02,380 And we can go ahead and call our flicker lights function. 356 00:23:02,380 --> 00:23:04,090 Now remember there's a while loop in there. 357 00:23:04,090 --> 00:23:07,600 So we don't want to get um, the thread of execution stuck in that while loop. 358 00:23:07,600 --> 00:23:13,090 So we can use task dot spawn and spawn our flicker lights function in a new thread. 359 00:23:13,090 --> 00:23:16,450 And then after that we can wait like two seconds. 360 00:23:16,450 --> 00:23:21,760 And then we're going to use our message UI event and fire to all the players to send out a new message. 361 00:23:21,760 --> 00:23:25,330 So actually we can just copy what we did right here. 362 00:23:26,540 --> 00:23:30,830 And paste that here because it's going to be Squidward speaking same image. 363 00:23:30,830 --> 00:23:37,130 And instead the message table for this one is going to be, uh, messages dot Squidward two and if the 364 00:23:37,130 --> 00:23:41,270 cam was manipulated for message, we're going to set that equal to false because it wasn't. 365 00:23:41,980 --> 00:23:46,270 And that means we're also going to have to calculate how long it'll take for these messages to display 366 00:23:46,270 --> 00:23:47,440 on the screen. 367 00:23:47,440 --> 00:23:50,590 So wait duration is going to be equal. 368 00:23:50,590 --> 00:23:56,380 To calculate wait duration for text, we're going to pass messages dot Squidward two. 369 00:23:57,160 --> 00:24:03,100 The time in between the messages is going to be again three seconds and then 0.02 for time in between 370 00:24:03,100 --> 00:24:04,090 the characters. 371 00:24:04,360 --> 00:24:07,210 And then we're going to wait for this wait duration. 372 00:24:07,210 --> 00:24:12,640 And we're also going to add a little bit of extra time, like an extra, let's say six seconds. 373 00:24:12,640 --> 00:24:17,770 And after that time is up, we're going to go ahead and turn off the power inside of the building, 374 00:24:17,770 --> 00:24:23,980 and then we can use the game comms event and fire to all of the clients to start playing a new sound, 375 00:24:23,980 --> 00:24:27,370 which is going to be the ambience for when the lights are off. 376 00:24:27,370 --> 00:24:33,550 So again, inside of the sound service, inside of ambience we have eerie ambience one and this is going 377 00:24:33,550 --> 00:24:35,860 to loop in play while the lights are off. 378 00:24:37,060 --> 00:24:38,830 Yeah, sounds pretty spooky. 379 00:24:38,830 --> 00:24:43,090 So we're going to use the action Game Comms enum for the client. 380 00:24:43,120 --> 00:24:45,730 We want them to play a sound. 381 00:24:46,320 --> 00:24:50,160 And that sound is going to be in the sound service ambience. 382 00:24:50,460 --> 00:24:56,700 Eerie ambience one and play at point is going to be false. 383 00:24:57,420 --> 00:25:02,850 And then to do a funny reference in our game inside of the server storage and our other stuff folder, 384 00:25:02,850 --> 00:25:08,490 we actually have the hash slinging slasher in there, and we're going to set his parent to the workspace 385 00:25:08,490 --> 00:25:09,240 as well. 386 00:25:09,240 --> 00:25:12,210 So when the lights turn off, all you're going to see is him standing outside. 387 00:25:12,210 --> 00:25:14,400 You'll just see a couple set of red glowing eyes. 388 00:25:14,400 --> 00:25:18,210 I thought that'd be, you know, kind of funny to make them think that's who they're up against. 389 00:25:18,210 --> 00:25:20,130 But that's not actually who they're up against. 390 00:25:20,130 --> 00:25:24,480 So we set him to be inside of the workspace. 391 00:25:24,900 --> 00:25:27,630 So let me actually go find him real quick. 392 00:25:27,630 --> 00:25:30,450 Inside of server storage. 393 00:25:30,450 --> 00:25:30,810 Where is he? 394 00:25:30,840 --> 00:25:31,620 Here he is. 395 00:25:31,620 --> 00:25:35,850 If we set his parent to the workspace, here he is. 396 00:25:35,850 --> 00:25:37,170 He's just going to be standing out there. 397 00:25:37,170 --> 00:25:38,850 This is just some free model that I found. 398 00:25:38,850 --> 00:25:42,450 I thought it would be kind of funny, but when they're inside of the restaurant, they'll just see a 399 00:25:42,450 --> 00:25:44,220 pair of red glowing eyes out there. 400 00:25:44,220 --> 00:25:45,270 It'd be kind of neat. 401 00:25:45,270 --> 00:25:50,790 Anyway, we can move them back inside of his folder, and if we set them to the workspace, we're again 402 00:25:50,790 --> 00:25:55,110 going to wait for like, another two seconds here. 403 00:25:55,110 --> 00:25:59,490 And then we're going to use our message UI event one more time. 404 00:25:59,490 --> 00:26:05,190 And this time we want to send out the messages inside of the message three table or Squidward three 405 00:26:05,190 --> 00:26:07,500 and again we can calculate the wait duration. 406 00:26:07,500 --> 00:26:09,690 So wait duration is equal to. 407 00:26:10,110 --> 00:26:12,150 Let me actually just copy this here. 408 00:26:12,150 --> 00:26:15,360 And what we need to pass is Squidward three. 409 00:26:16,140 --> 00:26:20,820 And then we're going to go ahead and wait for this wait duration. 410 00:26:20,820 --> 00:26:23,790 And this is where we would go ahead and start moving, Squidward. 411 00:26:23,790 --> 00:26:29,250 But we're going to do that in the next lecture to wrap up the introduction section or the first section 412 00:26:29,250 --> 00:26:33,870 of our story, but we should go ahead and test out what we've gotten done so far. 413 00:26:34,580 --> 00:26:40,700 And actually, before we start testing our game, make sure you move your glow stick giver back into 414 00:26:40,700 --> 00:26:42,770 the other stuff folder. 415 00:26:42,800 --> 00:26:43,820 There we go. 416 00:26:43,820 --> 00:26:46,490 And now we can go ahead and test out our game. 417 00:26:47,270 --> 00:26:50,450 Okay, that I filled up my last task here. 418 00:26:50,450 --> 00:26:54,950 So once we finish this, the lights and the building should start to flicker. 419 00:26:55,850 --> 00:26:57,080 And there we go. 420 00:26:57,170 --> 00:27:00,260 The lights are flickering and Squidward is now talking to us. 421 00:27:00,260 --> 00:27:01,070 Oh, great. 422 00:27:01,070 --> 00:27:02,900 The storm is messing with the power. 423 00:27:02,900 --> 00:27:09,650 Hopefully we don't lose power completely, and eventually the entire lights in our building should shut 424 00:27:09,650 --> 00:27:10,310 off. 425 00:27:13,060 --> 00:27:14,590 And there we go. 426 00:27:15,190 --> 00:27:19,840 Our friend is all the way out there, standing creepily, and Squidward is talking to us. 427 00:27:19,840 --> 00:27:21,430 And we also got our glow sticks here. 428 00:27:21,430 --> 00:27:27,190 So if we click this boom, we get our nice glow stick to kind of allow us to see around the map. 429 00:27:27,190 --> 00:27:31,750 And this gives players, you know, a little bit of time and opportunity to walk around the map and 430 00:27:31,750 --> 00:27:33,100 collect some money. 431 00:27:33,100 --> 00:27:34,660 So that's going to be very nice. 432 00:27:37,750 --> 00:27:42,130 So other than that, our introduction or section one for the story is almost complete. 433 00:27:42,130 --> 00:27:45,820 We just need to finish up a few more things, such as moving Squidward into the basement. 434 00:27:45,820 --> 00:27:48,490 And other than that, we should be good to go for this lecture. 435 00:27:48,490 --> 00:27:50,110 I'll go ahead and see you in the next one.